package com.dinner.service;

import java.util.List;

import com.dinner.exception.SellerRegistrationException;
import com.dinner.model.Seller;
import com.dinner.model.SellerDao;
import com.dinner.util.DinnerFactory;
import com.dinner.util.IRegistrationValidator;
import com.dinner.util.PageHelper;

public class SellerServiceImpl implements SellerService {

	private SellerDao sellerDao = null;
	
	private IRegistrationValidator registrationValidator = null;
	
	/**
	 * @return the registrationValidator
	 */
	public IRegistrationValidator getRegistrationValidator() {
		return registrationValidator;
	}


	/**
	 * @param registrationValidator the registrationValidator to set
	 */
	public void setRegistrationValidator(
			IRegistrationValidator registrationValidator) {
		this.registrationValidator = registrationValidator;
	}


	/**
	 * @return the sellerDao
	 */
	public SellerDao getSellerDao() {
		return sellerDao;
	}


	/**
	 * @param sellerDao the sellerDao to set
	 */
	public void setSellerDao(SellerDao sellerDao) {
		this.sellerDao = sellerDao;
	}


	@Override
	public Seller getSeller(String sellerID, String password1, String password2, 
			String sellerName, String sellerAddress, String openTime, 
			String closeTime, String telephone, String email, String description) throws SellerRegistrationException {
		
		if (sellerDao.getSellerByID(sellerID) != null) {
			throw new SellerRegistrationException("Seller with same ID already exists.");
		}
		
		registrationValidator.validateRegistrationID(sellerID);
		registrationValidator.validateRegistrationPassword(password1, password2);
		String businessTime = registrationValidator.
								validateBusinessTime(openTime, closeTime);
		registrationValidator.validateRegistrationEmail(email);
		
		DinnerFactory factory = DinnerFactory.getFactory();
		Seller seller = factory.newSeller();
		
		seller.setSellerID(sellerID);
		seller.setAuthenticated(false);
		seller.setDescription(description);
		seller.setEmail(email);
		seller.setOpenTime(businessTime);
		seller.setPassword(password1);
		seller.setSellerAddress(sellerAddress);
		seller.setSellerName(sellerName);
		seller.setTelephone(telephone);
		
		return seller;
	}


	@Override public void saveSeller(Seller seller) {
		sellerDao.saveSeller(seller);
	}


	@Override public Seller getSellerByID(String sellerID) {
		return sellerDao.getSellerByID(sellerID);
	}
	
	@Override
	public List<Seller> getSellersByAuthentication(boolean authenticated) {
		return sellerDao.getSellersByAuthentication(authenticated);
	}
	
	@Override
	public void executeSellerAuthentication(String sellerID, boolean permission) {
		if (permission) {
			sellerDao.passAuthentication(sellerID);
		} else {
			sellerDao.removeSeller(sellerID);
		}
	}
	
	@Override public void removeSeller(String sellerID) {
		sellerDao.removeSeller(sellerID);
	}


	@Override public List<Seller> getAllSellers() {
		List<Seller> list = sellerDao.getAllSellers();
		return list;
	}


	@Override
	public List<Seller> getSellersByPage(PageHelper page) {
		List<Seller> list = sellerDao.SellersByPage(page);
		return list;
	}


	@Override
	public List<Seller> getSellersByAuthenticationAndPage(boolean auth, PageHelper p) {
		return sellerDao.getSellersByAuthenticationAndPage(auth, p);
	}

}
